package com.example.demo6.service;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/20 21:21
 */
import com.example.demo6.dao.InventoryDao;
import com.example.demo6.entity.Goods;
import com.example.demo6.entity.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class InventoryService implements InventoryDao{
    @Autowired
    private final JdbcTemplate jdbcTemplate;

    public InventoryService(JdbcTemplate jdbcTemplate){this.jdbcTemplate=jdbcTemplate;}

    @Override
    public int add(Inventory inventory) {
        String sql = "insert into inventory values(null,?,?)";
        // 2 调用方法实现
        Object[] args = {inventory.getName(),inventory.getQuantity()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(Inventory inventory) {
        String sql="delete from inventory where Id=?";
        Object[] args={inventory.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int updata(Inventory inventory) {
        String sql="update inventory set name=?,quantity=? where Id=?";
        Object[] args={
                inventory.getName(),inventory.getQuantity(),inventory.getId()
        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public Inventory findone(Inventory inventory) {
        String sql = "select * from inventory where Id="+inventory.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Inventory>(Inventory.class));

    }

    @Override
    public List<Inventory> findAll() {
        String sql ="select * from inventory";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Inventory.class));
    }}

